<?php
class C_Router_Route 
{
	private $_params = array();
	 
	public function match($request)
	{
		
		$query = $request->getQuery();
		if(array_key_exists('module',$query) 
		   || array_key_exists('action',$query)
		   || array_key_exists('method',$query))
		{
			$this->getParams($query);
			return true;   	
		}
		else 
		{
		
			if($this->hasHomePage($request))
			{
				$this->getParams($query);
				return true;
			}
			return false;
		}
	}
	private function getParams($query)
	{
		$default = get_default();
		if(empty($query['module']))
		{
			$query['module'] = $default['module'];
		}
		if(empty($query['action']))
		{
			$query['action'] = $default['action'];
		}
		if(empty($query['method']))
		{
			$query['method'] = $default['method'];
		}
		$this->_params = $query;
	}
	public function hasHomePage($request)
	{
		$uri = $request->getRequestUri();
		if($uri =='/' || preg_match("/^\/\w++\.php$/i",$uri) || preg_match("/^\/(?:\w++\.php)?(?:\?|#)/i",$uri))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	//TODO 传递规则时不对
	public function getUrl($module,  $action,  $method,  $params = array(),$query = array())
	{
		$default = get_default();
		$macArray = array();
		if($module != $default['module'])
		{
			$query_array['module'] = strtolower($module);
		}
		if($action != $default['action'])
		{
			$query_array['action'] = strtolower($action);
		}
		if($method != $default['method'])
		{
			$query_array['method'] = strtolower($method);
		}
		$merge_array = array_merge($query_array, $query, $params);
		$query_string =  http_build_query($merge_array); 
		if(empty($query_string))
		{
			//$url = "/index.php";
			$url = "/";
		}
		else 
		{
			//$url = "/index.php?".$querystr;
			$url = "/?".$query_string;
		}
		return $url;
	}

	public function getRequestParams()
	{
		return $this->_params;
	}
}

?>